package com.vvlive.caibao.smartcity.base;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

/**
 * Created by zhj on 2015/9/2.
 * 所有Fragment的父类
 */
public abstract class BaseFragment extends Fragment {

    private View mContentView;
    private Toast mToast;

    /**
     * 获取页面布局的资源id
     *
     * @return
     */
    public abstract int getContentRes();

    /**
     * 初始化Fragment
     */
    public abstract void initFragment();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mContentView = inflater.inflate(getContentRes(), container, false);
        initFragment();
        return mContentView;
    }

    /**
     * 查找控件
     * @param id
     * @param <T>
     * @return
     */
    public <T extends View> T findView(int id) {
        return (T) mContentView.findViewById(id);
    }

    public void toast(Object obj) {
        String msg = obj.toString();
        if (mToast == null) {
            mToast = Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT);
        }
        mToast.setText(msg);
        mToast.show();
    }

    public void log(String tag, Object obj) {
        Log.i(tag, obj.toString());
    }

    public void log(Object obj) {
        log("BaseFragment", obj.toString());
    }
}
